package com.welink.cmp.statemachine.machine;

import com.welink.base.statemachine.Action;
import com.welink.base.statemachine.StateMachine;
import com.welink.base.statemachine.builder.StateMachineBuilder;
import com.welink.base.statemachine.builder.StateMachineBuilderFactory;
import com.welink.cmp.statemachine.StateMachineIdContent;
import com.welink.cmp.statemachine.actions.HostActions;
import com.welink.cmp.statemachine.context.CommonCtx;
import com.welink.cmp.statemachine.context.HostCtx;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author yanwei li create  2024-06-02 22:58
 */
@Component
@Slf4j
public class HostMachine {

    @Autowired
    private HostActions hostActions;


    @PostConstruct
    public void init(){
        final StateMachineBuilder<HostState, HostEvent, HostCtx> builder = StateMachineBuilderFactory.create();
        // 出库  入库 -> 出库
        // 出库  故障 -> 出库
        builder.addTransitions()
                .fromAmong(HostState.FAULTY,HostState.IN_STOCKED)
                .to(HostState.DISPATCHED)
                .on(HostEvent.OUT_STOCK)
                .perform(hostActions.doAction());
        // 入库 出库->入库
        builder.addTransition()
                .from(HostState.DISPATCHED)
                .to(HostState.IN_STOCKED)
                .on(HostEvent.PUT_STOCK)
                .perform(hostActions.doAction());

        // 上架 入库 -> 上架
        builder.addTransition()
                .from(HostState.IN_STOCKED)
                .to(HostState.MOUNTED)
                .on(HostEvent.MOUNTED)
                .perform(hostActions.doAction());

        // 质检 上架 -> 空闲
        builder.addTransition()
                .from(HostState.MOUNTED)
                .to(HostState.IDLE)
                .on(HostEvent.QUALITY_CHECK_SUCCESS)
                .perform(hostActions.doAction());
        // 下架  空闲 -> 上架
        builder.addTransition()
                .from(HostState.IDLE)
                .to(HostState.MOUNTED)
                .on(HostEvent.UNMOUNTED)
                .perform(hostActions.doAction());
        // 质检失败 上架 -> 故障
        builder.addTransition()
                .from(HostState.MOUNTED)
                .to(HostState.FAULTY)
                .on(HostEvent.QUALITY_CHECK_FAIL)
                .perform(hostActions.doAction());

        // 分配 空闲 -> 分配
        builder.addTransition()
                .from(HostState.IDLE)
                .to(HostState.ALLOCATED)
                .on(HostEvent.ALLOCATED)
                .perform(hostActions.doAction());

        // 部署  分配 -> 投产
        builder.addTransition()
                .from(HostState.ALLOCATED)
                .to(HostState.ACTIVE)
                .on(HostEvent.DEPLOY)
                .perform(hostActions.doAction());

        // 故障  投产 -> 故障
        builder.addTransition()
                .from(HostState.ACTIVE)
                .to(HostState.FAULTY)
                .on(HostEvent.FAILED)
                .perform(hostActions.doAction());
        // 回收  投产 -> 空闲
        builder.addTransition()
                .from(HostState.ACTIVE)
                .to(HostState.IDLE)
                .on(HostEvent.RECYCLED)
                .perform(hostActions.doAction());
        // 回收  故障 -> 回收
        builder.addTransition()
                .from(HostState.FAULTY)
                .to(HostState.RETIRED)
                .on(HostEvent.REPAIR_FAILED)
                .perform(hostActions.doAction());


        // 设备到期  故障 -> 回收
        builder.addTransition()
                .from(HostState.FAULTY)
                .to(HostState.RETIRED)
                .on(HostEvent.EXPIRED)
                .perform(hostActions.doAction());

        final StateMachine<HostState, HostEvent, HostCtx> build = builder.build(StateMachineIdContent.host_state_machine_id);
        log.info("初始化通用状态机已完成 successful ··· ···");

    }

}